package com.scoresystem.web.controller.dispatcher;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.scoresystem.common.CommonValue;
import com.scoresystem.web.service.LoginService;
import com.scoresystem.web.ui.bridge.ViewBridgeImpl;
import com.scoresystem.web.ui.view.ShowAdminMainView;
import com.scoresystem.web.ui.view.ShowStudentMainView;
import com.scoresystem.web.ui.view.ShowTeacherMainView;
import com.scoresystem.web.ui.view.ViewFactory;

import net.sf.json.JSONObject;
/**
 * 
 * @author Administrator
 *用于处理按职位登录的分发器
 */
public class LoginDispatcher extends BaseDispatcher implements IDispatcher {
	private LoginService service=new LoginService();
	public LoginDispatcher(){
		showable=null;
		viewbridge=new ViewBridgeImpl();
	}
	@Override
	public void dispatch(String option, HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		if(option.equals(CommonValue.REQUEST_OP_LOGIN)){
			//跳转到管理员主界面
			if(request.getParameter("post").equals(CommonValue.POST_ADMIN)){
				showable=ViewFactory.createShowAdminMainView();
				JSONObject json=service.checkoutAdminLogin
						(request.getParameter("username"), request.getParameter("password"));
				viewbridge.setShowable(showable).showView(json, request, response);
			}
			//跳转到教师主界面
			else if(request.getParameter("post").equals(CommonValue.POST_TEACHER)){
				showable=ViewFactory.createShowTeacherMainView();
				JSONObject json=service.checkoutTeaLogin
						(request.getParameter("username"), request.getParameter("password"));
				viewbridge.setShowable(showable).showView(json, request, response);
			}
			//跳转到学生主界面
			else if(request.getParameter("post").equals(CommonValue.POST_STUDENT)){
				showable=ViewFactory.createShowStudentMainView();
				JSONObject json=service.checkoutStuLogin
						(request.getParameter("username"), request.getParameter("password"));
				viewbridge.setShowable(showable).showView(json, request, response);
			}
		}
		else if(option.equals(CommonValue.REQUEST_OP_UPDATEPASSWORD)){
			showable=ViewFactory.createShowUpdatePasswordView();
			JSONObject json=service.checkoutUpsd(request.getParameter("username"), request.getParameter("password")
					,request.getParameter("newpsd"));
			viewbridge.setShowable(showable).showView(json, request, response);
		}
	}

}
